package summaries;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

import utils.Interval;

public class Results {
	
	private boolean hasInterval = false;
	private double T1;
	private double T2;
	private double T3;
	private double T4;
	private double T5;
	private double T6;
	private double T7;
	private double T8;
	private double T9;
	private double T10;
	private double T11;
	private double T;
	
	private Interval T1_interval;
	private Interval T2_interval;
	private Interval T3_interval;
	private Interval T4_interval;
	private Interval T8_interval;
	private Interval T9_interval;
	private Interval T10_interval;
	private Interval T_interval;
	
	private String summary;
	
	public Results() {
		
	}
	
	public Results(double T1, double T2, double T3, double T4, double T5, double T6, double T7, double T8, double T9, double T10, double T11, double T, String summary) {
		this.T1  = T1;
		this.T2  = T2;
		this.T3  = T3;
		this.T4  = T4;
		this.T5  = T5;
		this.T6  = T6;
		this.T7  = T7;
		this.T8  = T8;
		this.T9  = T9;
		this.T10 = T10;
		this.T11 = T11;
		this.T   = T;
		this.summary = summary;
	}
	
	public Results(Interval T1, Interval T2, Interval T3, Interval T4, double T5, double T6, double T7, Interval T8, Interval T9, Interval T10, double T11, Interval T, String summary) {
		T1_interval = T1;
		T2_interval = T2;
		T3_interval = T3;
		T4_interval = T4;
		this.T5 = T5;
		this.T6 = T6;
		this.T7 = T7;
		T8_interval = T8;
		T9_interval = T9;
		T10_interval = T10;
		this.T11 = T11;
		T_interval = T;
		hasInterval = true;
		this.summary = summary;
		
		// Compute T
		double Tlower = T1.getX() * 1.0/11.0 + T2.getX() * 1.0/11.0 + T3.getX() * 1.0/11.0 + T4.getX() * 1.0/11.0 + T5 * 1.0/11.0 +
						T6 * 1.0/11.0 + T7* 1.0/11.0 + T8.getX() * 1.0/11.0 + T9.getX() * 1.0/11.0 + T10.getX() * 1.0/11.0 +
						T11 * 1.0/11.0;
		
		T.setX(Tlower);
		
		double Tupper = T1.getY() * 1.0/11.0 + T2.getY() * 1.0/11.0 + T3.getY() * 1.0/11.0 + T4.getY() * 1.0/11.0 + T5 * 1.0/11.0 +
				T6 * 1.0/11.0 + T7 * 1.0/11.0 + T8.getY() * 1.0/11.0 + T9.getY() * 1.0/11.0 + T10.getY() * 1.0/11.0 +
				T11 * 1.0/11.0;

		T.setY(Tupper);
	}
	
	public void show() {
		if (!hasInterval) {
			System.out.println(summary);
			System.out.println("T1  = " + T1_interval);
			System.out.println("T2  = " + T2_interval);
			System.out.println("T3  = " + T3_interval);
			System.out.println("T4  = " + T4_interval);
			System.out.println("T5  = " + T5);
			System.out.println("T6  = " + T6);
			System.out.println("T7  = " + T7);
			System.out.println("T8  = " + T8_interval);
			System.out.println("T9  = " + T9_interval);
			System.out.println("T10 = " + T10_interval);
			System.out.println("T11 = " + T11);
			System.out.println("T   = " + T_interval);
		}
	}
	
	@Deprecated
	/**
	 * This method is deprecated. Use public String getLineForFile instead.
	 * @param filename
	 */
	public void appendToFile(String filename) {
		DecimalFormatSymbols symbols = new DecimalFormatSymbols();
		symbols.setDecimalSeparator('.');
		DecimalFormat twoDForm = new DecimalFormat("#.###", symbols);
		try {
			FileWriter fstream = new FileWriter(filename, true);
			BufferedWriter out = new BufferedWriter(fstream);
			String line = "";
			summary = summary.substring(0, summary.length() - 1);
			if (hasInterval) {
				line = summary+","+twoDForm.format(T1_interval.getX())+" - "+twoDForm.format(T1_interval.getY())+","+
								   twoDForm.format(T2_interval.getX())+" - "+twoDForm.format(T2_interval.getY())+","+
								   twoDForm.format(T3_interval.getX())+" - "+twoDForm.format(T3_interval.getY())+","+
								   twoDForm.format(T4_interval.getX())+" - "+twoDForm.format(T4_interval.getY())+","+
								   twoDForm.format(T5)+","+
								   twoDForm.format(T6)+","+
								   twoDForm.format(T7)+","+
								   twoDForm.format(T8_interval.getX())+" - "+twoDForm.format(T8_interval.getY())+","+
								   twoDForm.format(T9_interval.getX())+" - "+twoDForm.format(T9_interval.getY())+","+
								   twoDForm.format(T10_interval.getX())+" - "+twoDForm.format(T10_interval.getY())+","+
								   twoDForm.format(T11)+","+
								   twoDForm.format(T_interval.getX())+" - "+twoDForm.format(T_interval.getY())+"\n";
				//System.out.println(line);
			} else {
				line = summary+","+twoDForm.format(T1)+","+twoDForm.format(T2)+","+twoDForm.format(T3)+","+twoDForm.format(T4)+","+twoDForm.format(T5)+","+twoDForm.format(T6)+","+twoDForm.format(T7)+","+twoDForm.format(T8)+","+twoDForm.format(T9)+","+twoDForm.format(T10)+","+twoDForm.format(T11)+","+twoDForm.format(T)+"\n";
			}
			out.write(line);
			out.close();
		} catch (Exception e) {
			
		}
	}
	
	public String getLineForFile() {
		DecimalFormatSymbols symbols = new DecimalFormatSymbols();
		symbols.setDecimalSeparator('.');
		DecimalFormat twoDForm = new DecimalFormat("#.###", symbols);
		String line = "";
		summary = summary.substring(0, summary.length() - 1);
		if (hasInterval) {
			line = summary+","+twoDForm.format(T1_interval.getX())+" - "+twoDForm.format(T1_interval.getY())+","+
							   twoDForm.format(T2_interval.getX())+" - "+twoDForm.format(T2_interval.getY())+","+
							   twoDForm.format(T3_interval.getX())+" - "+twoDForm.format(T3_interval.getY())+","+
							   twoDForm.format(T4_interval.getX())+" - "+twoDForm.format(T4_interval.getY())+","+
							   twoDForm.format(T5)+","+
							   twoDForm.format(T6)+","+
							   twoDForm.format(T7)+","+
							   twoDForm.format(T8_interval.getX())+" - "+twoDForm.format(T8_interval.getY())+","+
							   twoDForm.format(T9_interval.getX())+" - "+twoDForm.format(T9_interval.getY())+","+
							   twoDForm.format(T10_interval.getX())+" - "+twoDForm.format(T10_interval.getY())+","+
							   twoDForm.format(T11)+","+
							   twoDForm.format(T_interval.getX())+" - "+twoDForm.format(T_interval.getY())+"\n";
			//System.out.println(line);
		} else {
			line = summary+","+twoDForm.format(T1)+","+twoDForm.format(T2)+","+twoDForm.format(T3)+","+twoDForm.format(T4)+","+twoDForm.format(T5)+","+twoDForm.format(T6)+","+twoDForm.format(T7)+","+twoDForm.format(T8)+","+twoDForm.format(T9)+","+twoDForm.format(T10)+","+twoDForm.format(T11)+","+twoDForm.format(T)+"\n";
		}
		return line;
	}
	
	public String toString() {
		if (hasInterval) {
			return toStringInterval();
		} else {
			return toStringNonInterval();
		}
	}
	
	public String toStringNonInterval() {
		DecimalFormatSymbols symbol = new DecimalFormatSymbols();
		symbol.setDecimalSeparator('.');
		DecimalFormat twoDForm = new DecimalFormat("0.000", symbol);
		
		String s = "";
		s = twoDForm.format(T1) + "\t" + twoDForm.format(T2) + "\t" + twoDForm.format(T3) + "\t" + twoDForm.format(T4) + "\t" + 
			twoDForm.format(T5) + "\t" + twoDForm.format(T6) + "\t" + twoDForm.format(T7) + "\t" + twoDForm.format(T8) + "\t" +
			twoDForm.format(T9) + "\t" + twoDForm.format(T10) + "\t" + twoDForm.format(T11) + "\t" + twoDForm.format(T);
		return s;
	}
	
	public String toStringInterval() {
		DecimalFormatSymbols symbol = new DecimalFormatSymbols();
		symbol.setDecimalSeparator('.');
		DecimalFormat twoDForm = new DecimalFormat("0.000", symbol);
		
		String s = "";
		s = T1_interval + "\t" + T2_interval + "\t" + T3_interval + "\t" + T4_interval + "\t" + twoDForm.format(T5) + "\t" +
			twoDForm.format(T6) + "\t" + twoDForm.format(T7) + "\t" + T8_interval + "\t" + T9_interval + "\t" + T10_interval + "\t" +
			twoDForm.format(T11) + "\t" + T_interval;	
		return s;
	}
	
}
